package com.jqbd.service.impl;

import com.alibaba.excel.EasyExcel;
import com.jqbd.dto.ResumeMapData;
import com.jqbd.listener.ResumeListent;
import com.jqbd.mapper.ResumeMapper;
import com.jqbd.pojo.Resume;
import com.jqbd.service.ResumeService;
import com.jqbd.vo.LacVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResumeServiceImpl implements ResumeService {
    @Autowired
    private ResumeMapper resumeMapper;

    @Override
    public List<Resume> findAll(Map map) {
        System.out.println("11111111111");
        return resumeMapper.findAll(map);
    }

    @Override
    public void findById(String id){

        System.out.println("11111111111");
         resumeMapper.findById(id);
    }

    @Override
    public void add(Resume resume) {
        resumeMapper.add(resume);
    }
    @Override
    public int deleteById(int id) {
       return resumeMapper.deleteById(id);
    }

    @Override
    public int update(Resume resume) {
        return resumeMapper.update(resume);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return resumeMapper.deleteByIds(ids);
    }
    @Override
    public void writeExcel(Integer[] ids, HttpServletResponse response) {
        //判断
        if(ids.length > 0){
            //查询数据库
            List<Resume> patientList = resumeMapper.findByIds(ids);
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            //System.out.println("当前系统时间戳：" + System.currentTimeMillis());
            // 写法2
            //String fileName = "D:/patient-" + System.currentTimeMillis() + ".xlsx";
            //String fileName = "D:/patient-" + currTime + ".xlsx";
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            //EasyExcel.write(fileName, Patient.class).sheet("患者列表").doWrite(patientList);

            //通过下载的方式导出Excel文件
            String fileName = "patient-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, Resume.class).sheet("患者列表").doWrite(patientList);
        }else {
            //如果没有选择性导出数据，那么就导出所有
            //查询数据库

            List<Resume> patientList = resumeMapper.findAllNoparm();
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            //通过下载的方式导出Excel文件
            String fileName = "patient-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, Resume.class).sheet("简历列表").doWrite(patientList);
        }
    }

    @Override
    public void save(MultipartFile file) {

        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        // 写法3：
        //String fileName = "D:/patient-2022-11-08.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        //EasyExcel.read(fileName, Patient.class, new PatientListener()).sheet().doRead();

        InputStream inputStream = null;

        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取上传的Excel文件

        EasyExcel.read(inputStream, Resume.class,
               new ResumeListent(resumeMapper)).sheet().doRead();

    }

    @Override
    public List<ResumeMapData> getResumeMapData() {
        return resumeMapper.getResumeMapData();
    }
    @Override
    public Map<Object, Object> stat() {
        List<LacVo> list = resumeMapper.stat();
        Map<Object, Object> map = new HashMap<>();
        map.put("data", list);
        return map;

    }
}
